import com.xx.bean.Book;
import com.xx.bean.User;
import com.xx.mapper.BookMapper;
import com.xx.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class DEMO01 {
    @Test
    public void testHelloWorld() throws IOException {
        //设置MyBatis全局配置文件的路径
        String resource = "mybatis-config.xml";
        //读取类路径下的配置文件得到输入流
        InputStream inputStream = Resources.getResourceAsStream(resource);
        //创建SqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //获取SqlSession对象，相当于Connection对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try {
            //获取Mapper代理对象
//            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//            //调用EmployeeMapper中获取一个对象的方法
//           mapper.addUser(new User(null,"123445","23123","12323"));
//            sqlSession.commit();
            BookMapper mapper = sqlSession.getMapper(BookMapper.class);
            List<Book> books = mapper.listBook();
            System.out.println(books);
        } finally {
            //关闭sqlSession
            sqlSession.close();
        }
    }
}


